﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;

public partial class Ctl_Q_State : System.Web.UI.UserControl
{
    private int SurfaceCount = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        

    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        
    }
    public void MyLoad()
    {
        object d = Source;
        RenderHeader();

    }

    public string btnOk_ClientID { get { return string.Empty ; } }    
    
    public string BtnCancel_ClientID { get
    {
        //return btnClose.ClientID.Replace("_", "$");
        return btnClose.ClientID;
    } 
    }

    public string SideId { get; set; }
    public DateTime  dtStart { get; set; }
    public DateTime dtFinish { get; set; }

    private void RenderHeader()
    {
        wsConstractions ws = (wsConstractions)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Constraction];                    
        
        Constraction constr= ws.GetConstractionFromSideId(SideId);
        Side s = (Side)constr.SideList[SideId];
        SurfaceCount = s.SurfaceCount;
        lblGID.Text = constr.GID;
        lblLitera .Text =s.Litera ;
        lblStart.Text = dtStart.ToShortDateString();
        lblEnd.Text = dtFinish.ToShortDateString();
        
        lvContractSides.DataSource = Source;
        lvContractSides.DataBind();
    }

    protected bool GetIsQPref(object  Item)
    {
        ContractSurfaceItem i= (ContractSurfaceItem)Item ;
        return i.qPrioritetLevel > 0;

    }

    protected string GetCssRow(object Item)
    { 
        ContractSurfaceItem i= (ContractSurfaceItem)Item ;
        if (i.SurfaceNumber > SurfaceCount-1)
                    return "grd_td_item";
        return "grd_td_item_caption";
                
    }
    
        protected string  GetStatus(object Item)
        {
            Contract c = GetContract(Item);
            return c.ContractStatus.StatusName ; 
        }

        protected string GetStatusHint(object Item)
        {
            Contract c = GetContract(Item);
            if (c.ContractStatus is ContractStatus_Reserv )
                return "Зарезервированно до:" + ((ContractStatus_Reserv)c.ContractStatus).dtFinishReserv.ToShortDateString();
            return string.Empty;
        }

    protected Contract GetContract(object Item)
    {
        ContractSurfaceItem i= (ContractSurfaceItem)Item ;

        wsContract ws = (wsContract)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Contract]; 
        Contract c= ws.GetContract (i.ContractID );        
        
        
        return c;
    }

    protected string GetClientName(object Item)
    {
        Contract c = GetContract(Item);
        if (null==c.Client ) return "н/д";
        return c.Client.FullName;
 
    }

    public ContractSurfaceItem[]  Source
    {
        get 
        {
             
            ContractSurface_DistrubeItemList List = new ContractSurface_DistrubeItemList();
            List.DtStart =dtStart  ;
            List.DtEnd = dtFinish ;
            List.SideID = SideId ;
            List.Load();
            ContractSurfaceItem[] tbl= List.ToArray<ContractSurfaceItem>();
            var r =
            from n in tbl
            orderby n.SurfaceNumber 
            select n;

            return r.ToArray<ContractSurfaceItem>();

        }
    }

}
